core.date¶
This module provides functions for date and time conversions. The functions in this module can accept any of the following date types:
java.time.ZonedDateTime
java.time.LocalDateTime
java.util.Calendar
java.util.Date
org.joda.time.DateTime
datetime.datetime (Python)
org.eclipse.smarthome.core.library.types.DateTimeType
org.openhab.core.library.types.DateTimeType
-
core.date.
format_date
(value, format_string="yyyy-MM-dd'T'HH:mm:ss.SSxx")¶ Returns string of
value
formatted according toformat_string
.This function can be used when updating Items in openHAB or to format any date value for output. The default format string follows the same ISO8601 format used in openHAB. If
value
does not have timezone information, the system default will be used.Examples
events.sendCommand("date_item", format_date(date_value)) log.info("The time is currently: {}".format(format_date(ZonedDateTime.now())))
- Parameters
value – the value to convert
format_string (str) – the pattern to format
value
with. See java.time.format.DateTimeFormatter for format string tokens.
- Returns
the converted value
- Return type
str
-
core.date.
days_between
(value_from, value_to, calendar_days=False)¶ Returns the number of days between
value_from
andvalue_to
. Will return a negative number ifvalue_from
is aftervalue__to
.Examples
span_days = days_between(items["date_item"], ZonedDateTime.now())
- Parameters
value_from – value to start from
value_to – value to measure to
calendar_days (bool) – if
True
, the value returned will be the number of calendar days rather than 24-hour periods (default)
- Returns
the number of days between
value_from
andvalue_to
- Return type
int
-
core.date.
hours_between
(value_from, value_to)¶ Returns the number of hours between
value_from
andvalue_to
. Will return a negative number ifvalue_from
is aftervalue__to
.Examples
span_hours = hours_between(items["date_item"], ZonedDateTime.now())
- Parameters
value_from – value to start from
value_to – value to measure to
- Returns
the number of hours between
value_from
andvalue_to
- Return type
int
-
core.date.
minutes_between
(value_from, value_to)¶ Returns the number of minutes between
value_from
andvalue_to
. Will return a negative number ifvalue_from
is aftervalue__to
.Examples
span_minutes = minutes_between(items["date_item"], ZonedDateTime.now())
- Parameters
value_from – value to start from
value_to – value to measure to
- Returns
the number of minutes between
value_from
andvalue_to
- Return type
int
-
core.date.
seconds_between
(value_from, value_to)¶ Returns the number of seconds between
value_from
andvalue_to
. Will return a negative number ifvalue_from
is aftervalue__to
.Examples
span_seconds = seconds_between(items["date_item"], ZonedDateTime.now())
- Parameters
value_from – value to start from
value_to – value to measure to
- Returns
the number of seconds between
value_from
andvalue_to
- Return type
int
-
core.date.
to_java_zoneddatetime
(value)¶ Converts any of the supported date types to
java.time.ZonedDateTime
. Ifvalue
does not have timezone information, the system default will be used.Examples
java_time = to_java_zoneddatetime(items["date_item"])
- Parameters
value – the value to convert
- Returns
the converted value
- Return type
java.time.ZonedDateTime
- Raises
TypeError – if the type of
value
is not supported by this module
-
core.date.
to_python_datetime
(value)¶ Converts any of the supported date types to Python
datetime.datetime
. Ifvalue
does not have timezone information, the system default will be used.Examples
python_time = to_python_datetime(items["date_item"])
- Parameters
value – the value to convert
- Returns
the converted value
- Return type
datetime.datetime
- Raises
TypeError – if the type of
value
is not supported by this module
-
core.date.
to_joda_datetime
(value)¶ Converts any of the supported date types to
org.joda.time.DateTime
. Ifvalue
does not have timezone information, the system default will be used.Examples
joda_time = to_joda_datetime(items["date_item"])
- Parameters
value – the value to convert
- Returns
the converted value
- Return type
org.joda.time.DateTime
- Raises
TypeError – if the type of
value
is not suported by this package
-
core.date.
to_java_calendar
(value)¶ Converts any of the supported date types to
java.util.Calendar
. Ifvalue
does not have timezone information, the system default will be used.Examples
calendar_time = to_java_calendar(items["date_item"])
- Parameters
value – the value to convert
- Returns
the converted value
- Return type
java.util.Calendar
- Raises
TypeError – if the type of
value
is not supported by this package
-
core.date.
human_readable_seconds
(seconds)¶ Converts seconds into a human readable string of days, hours, minutes and seconds.
Examples
message = human_readable_seconds(55555) # 15 hours, 25 minutes and 55 seconds
- Parameters
seconds – the number of seconds
- Returns
a string in the format
{} days, {} hours, {} minutes and {} seconds
- Return type
str